package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JInternalFrame;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.DefaultTableModel;

public class Insert extends JInternalFrame {
	DefaultTableModel model;
	JTable table;
	Select sel;
	DatabaseTest d;
	
	Connection conn;
	JTextField textCode;
	JTextField textEa;
	private JButton btnNewButton;
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel lblstate;

	/**
	 * Create the frame.
	 */
	public Insert(Connection conn, DatabaseTest d) {
		super("Insert", false, true, false, false);

//		this.sel=sel;
		this.d=d;
		//인터날 프레임 킬때 이벤트 ! 
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameActivated(InternalFrameEvent e) {
				textCode.setText("");
				textEa.setText("");
			}
		});
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.conn=conn;
		setBounds(0, 0, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getTextCode());
		getContentPane().add(getTextEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLblstate());

	}

	private JTextField getTextCode() {
		if (textCode == null) {
			textCode = new JTextField();
			textCode.setBounds(123, 58, 116, 21);
			textCode.setColumns(10);
		}
		return textCode;
	}

	private JTextField getTextEa() {
		if (textEa == null) {
			textEa = new JTextField();
			textEa.setBounds(123, 112, 116, 21);
			textEa.setColumns(10);
		}
		return textEa;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("입력");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						
						//ptbl_serial.nextval 시퀀스 
						String sql1 = "insert into ptbl(serial,code,ea) values (ptbl_serial.nextval,?,?)";
 						PreparedStatement pstmt = conn.prepareStatement(sql1);
 						
 						pstmt.setString(1, textCode.getText());
 						pstmt.setInt(2, Integer.parseInt(textEa.getText()));

						pstmt.executeUpdate();

						lblstate.setText("입력완료!");
						
						d.sel.QueryStart();
						
						
					} catch (Exception ee) {

					}
				}
			});
			btnNewButton.setBounds(285, 57, 97, 76);
		}
		return btnNewButton;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("코드");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(57, 61, 57, 15);
		}
		return lblNewLabel;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("수량");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setBounds(57, 115, 57, 15);
		}
		return label;
	}

	private JLabel getLblstate() {
		if (lblstate == null) {
			lblstate = new JLabel("");
			lblstate.setBackground(new Color(255, 255, 255));
			lblstate.setOpaque(true);
			lblstate.setBounds(57, 155, 325, 44);
		}
		return lblstate;
	}
}
